'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:ManageRepayCtrl
 * @description
 * # ManageRepayCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('ManageRepayCtrl', function ($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id

        $scope.showcerList = function () {
            var param = {};
            param.url = openApi.performanceListA;
            param.params = {
                id: $scope.param.performanceApplyId
            };
            Util.getPerformanceList(param, function (data) {
                $scope.performanceList = data.result
            })

        };
        $scope.chooseProList = function () {
            var cerReference = {
                name: "ManageRepayCtrl",
                selectTab: "performanceReturn_select_performance",
                selectTabContent: "工程名称"
            };
            var params = $scope.param;
            Util.savaParamForReview(params, id);
            Util.savaDataforsearch(cerReference);

            $location.path("finance/choosepeople")
        };
        var selectLists = [];
        $scope.toggle = function (item) {
            var curItemIndex;
            item.selected = !item.selected;
            selectLists = $scope.performanceList.filter(function (item, index) {
                return item.selected;
            })

            $scope.param.detailsStr = getNames();
        };
        var getNames = function () {
            var arr = [];
            if (selectLists.length > 0) {
                selectLists.forEach(function (item, index) {
                    arr.push(item.perName)
                })
            }
            return arr.join();
        };
        var getDetails = function () {
            var detailsArr = []
            selectLists.forEach(function (item, index) {
                detailsArr.push(item.pkId)
            });
            return detailsArr.join();
        };
        $scope.submit = function (operationId) {
            $scope.param.perIds = getDetails() || $scope.param.perIds
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.performanceReturn;
            options.param = angular.copy($scope.param, {});
            FormSubmit.submit(options,$scope);
        };
        $scope.validate = function () {
            if (!$scope.param.remark) {
                Util.Toast("请填写备注");
                return;
            }
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if (selectLists.length < 1 && !$scope.param.perIds) { //如果是回显的数据会有perids的
                Util.Toast("请填选择证明材料");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }

        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                if ($scope.param.performanceId) {
                    $scope.showcerList();
                }
            });
        };
        // 初始化
        init();
    });